import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanDeactivate, Router, RouterStateSnapshot } from '@angular/router';

import { LockScreenService, LockScreenState } from './lock-screen.service';

/*守卫锁屏页面*/
@Injectable({
  providedIn: 'root'
})
export class LockScreenLeaveGuard implements CanDeactivate<unknown> {
  private lockScreenState!: LockScreenState;

  constructor(private router: Router, private lockScreenSrc: LockScreenService) {
    lockScreenSrc.onLockScreenStateChanged.subscribe(data => (this.lockScreenState = data));
  }

  canDeactivate(component: unknown, currentRoute: ActivatedRouteSnapshot, currentState: RouterStateSnapshot, nextState?: RouterStateSnapshot): boolean {
    return !this.lockScreenState.locked;
  }
}
